<?php
class User extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->load->helper("level_helper");
        $this->_data["loadPage"]="user/index_view";
        $this->_data["titlePage"]="Quản lý user";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_username");
        $this->session->unset_userdata("sess_search_fullname");
        $this->session->unset_userdata("sess_search_address");
        $this->session->unset_userdata("sess_search_phone");
        $this->session->unset_userdata("sess_search_email");
        $this->session->unset_userdata("sess_search_level");
        
        $this->load->model("muser");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/user/index/",
            "total_rows"=>$this->muser->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->muser->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->muser->listAll($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        
        $this->load->model("mlevel");
        if($this->mlevel->listAll() == false)
            $this->_data["sl_level"]="empty";
        else
            $this->_data["sl_level"]=$this->mlevel->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        $username=$this->input->post("username");
        $pass=$this->input->post("pass");
        $fullname=$this->input->post("fullname");
        $address=$this->input->post("address");
        $phone=$this->input->post("phone");
        $email=$this->input->post("email");
        $level=$this->input->post("level");
        $this->load->model("muser");
        if($this->muser->checkUsername($username) == false)
            echo "username";
        else if($this->muser->checkEmail($email) == false)
            echo "email";
        else {
            $data=array(
                "u_username"=>$username,
                "u_password"=>md5($pass),
                "u_fullname"=>$fullname,
                "u_address"=>$address,
                "u_phone"=>$phone,
                "u_email"=>$email,
                "level_id"=>$level
            );
            $this->muser->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm user thành công.");
            echo "Thêm thành viên thành công!";
        }
    }
    
    public function edit() {
        $this->load->helper("level_helper");
        $this->_data["loadPage"]="user/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa user";
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("muser");
        $temp=$this->muser->getById($id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        $this->load->model("mlevel");
        if($this->mlevel->listAll() == false)
            $this->_data["sl_level"]="empty";
        else
            $this->_data["sl_level"]=$this->mlevel->listAll();
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtUsername",
                "label"=>"Username",
                "rules"=>"callback_checkUsername"
            ),
            array(
                "field"=>"txtEmail",
                "label"=>"Email",
                "rules"=>"callback_checkEmail"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "u_username"=>$this->input->post("txtUsername"),
                "u_fullname"=>$this->input->post("txtFullname"),
                "u_address"=>$this->input->post("txtAddress"),
                "u_phone"=>$this->input->post("txtPhone"),
                "u_email"=>$this->input->post("txtEmail"),
                "level_id"=>$this->input->post("slLevel")
            );
            if($this->input->post("txtPass")!="")
                $data["u_password"]=$this->input->post("txtPass");
            $this->muser->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa user thành công.");
            redirect(base_url()."admin_combo/user/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("muser");
        $this->muser->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa user thành công.");
        redirect(base_url()."admin_combo/user/");
    }
    
    public function search() {
        $this->load->helper("level_helper");
        $this->_data["loadPage"]="user/search_view";
        $this->_data["titlePage"]="Tìm kiếm user";
        
        $username=$this->session->userdata("sess_search_username");
        $fullname=$this->session->userdata("sess_search_fullname");
        $address=$this->session->userdata("sess_search_address");
        $phone=$this->session->userdata("sess_search_phone");
        $email=$this->session->userdata("sess_search_email");
        $level=$this->session->userdata("sess_search_level");
        
        $this->load->model("muser");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/user/search/",
            "total_rows"=>$this->muser->countAllResults($username, $fullname, $address, $phone, $email, $level)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->muser->search($username, $fullname, $address, $phone, $email, $level, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->muser->search($username, $fullname, $address, $phone, $email, $level, $config["per_page"], $start);
        
        $this->load->model("mlevel");
        if($this->mlevel->listAll() == false)
            $this->_data["sl_level"]="empty";
        else
            $this->_data["sl_level"]=$this->mlevel->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchUser"])) {
            $sess=array(
                "sess_search_username"=>$this->input->post("txtUsername"),
                "sess_search_fullname"=>$this->input->post("txtFullname"),
                "sess_search_address"=>$this->input->post("txtAddress"),
                "sess_search_phone"=>$this->input->post("txtPhone"),
                "sess_search_email"=>$this->input->post("txtEmail"),
                "sess_search_level"=>$this->input->post("slLevel")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/user/search");
        }
    }
    
    public function reloadData() {
        $this->load->model("muser");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/user/index",
            "total_rows"=>$this->muser->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->muser->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->muser->listAll($config["per_page"], $start);
        $this->load->view("user/reload_view", $this->_data);
    }
    
    public function checkUsername($username) {
        $this->load->model("muser");
        $id=$this->uri->segment(4);
        if(!$this->muser->checkUsername($username, $id)) {
            $this->form_validation->set_message("checkUsername", "Username này đã được đăng ký!");
            return false;
        }else
            return true;
    }
    
    public function checkEmail($email) {
        $this->load->model("muser");
        $id=$this->uri->segment(4);
        if(!$this->muser->checkEmail($email, $id)) {
            $this->form_validation->set_message("checkEmail", "Email này đã được đăng ký!");
            return false;
        }else
            return true;
    }
}
?>
